package main

import "github.com/kataras/iris/core/errors"

//音乐结构体
type MusicEntry struct {
	Id string
	Name string
	Artist string
	Source string
	Type string
}

type MusicManager struct {
	musics []MusicEntry
}

func NewMusicManager() *MusicManager {
	return &MusicManager{make([]MusicEntry, 0)}
}

//music长度
func (m *MusicManager) Len() int {
	return len(m.musics)
}

//取出某一个music
func (m *MusicManager) Get(index int) (music *MusicEntry, err error) {
	if index<0 || index> len(m.musics) {
		return nil, errors.New("index超过长度")
	}
	return &m.musics[index], nil
}

//找出某一个
func (m *MusicManager) Find(name string) *MusicEntry {
	if len(m.musics) == 0 {
		return nil
	}

	for _,m := range m.musics  {
		if m.Name == name {
			return &m
		}
	}
	return nil
}

//添加music
func (m *MusicManager) Add(music *MusicEntry) {
	m.musics = append(m.musics, *music)
}

//删除music
func (m *MusicManager) Remove(index int) *MusicEntry {
	if index<0 || index>=len(m.musics) {
		return nil
	}

	removeMusic := &m.musics[index]
	m.musics = append(m.musics[:index],m.musics[index+1:]...)
	return removeMusic
}



